package com.anycc.cloud.common.dto.query;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * paging response
 * @param <T> T
 */
@Data
public class PagingResponse<T> {
    private int page;
    private int size;
    private int totalPages;
    private long totalElements;
    private List<T> datas;

    public PagingResponse() {
        this.datas = new ArrayList<>();
    }

    public PagingResponse(int page, int size, int totalPages, long totalElements, List<T> datas) {
        this.datas = new ArrayList<>();
        this.page = page;
        this.size = size;
        this.totalElements = totalElements;
        this.totalPages = totalPages;
        this.datas = datas;
    }

    public PagingResponse(PagingRequest pagingRequest, int totalPages, long totalElements, List<T> datas) {
        this(pagingRequest.getPage(), pagingRequest.getSize(), totalPages, totalElements, datas);
    }

    public PagingResponse(PagingRequest pagingRequest, long totalElements, List<T> datas) {
        this.datas = new ArrayList<>();
        this.page = pagingRequest.getPage();
        this.size = pagingRequest.getSize();
        this.totalPages = this.getSize() == 0 ? 1 : (int)Math.ceil((double)totalElements / (double)this.getSize());
        this.totalElements = totalElements;
        this.datas = datas;
    }


    public int getOffset() {
        return this.page * this.size;
    }


}
